package com.zhongzhi.javafx.controller;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

import com.zhongzhi.common.Common;
import com.zhongzhi.common.CommonUpdata;
import com.zhongzhi.springboot.pojo.Station;
import com.zhongzhi.springboot.pojo.Equipment;
import com.zhongzhi.springboot.pojo.Station;
import com.zhongzhi.springboot.pojo.StationShow;
import com.zhongzhi.springboot.pojo.Type;
import com.zhongzhi.util.ClientUtil;
import com.zhongzhi.util.JsonResult;
import com.zhongzhi.util.PageUtil;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.layout.HBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.Callback;
import lombok.extern.log4j.Log4j;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.control.TableView;
@Log4j
public class StationViewController implements Initializable{
	@FXML
	private TableView TableAs;
	@FXML
	private Button NewAs;
	@FXML
	private HBox hbox;
	@FXML
	private Button startbutton;
	@FXML
	private Button upbutton;
	@FXML
	private TextField num;
	@FXML
	private Button skip;
	@FXML
	private Button downbutton;
	@FXML
	private Button endbutton;
	
	private int pages = 1;			//当前页
	private int number = 50;		//当前页行数
	private int TableNums = 5;		//总记录数
	private int TablePages = 5;		//总页数

	@Override
	public void initialize(URL location, ResourceBundle resources) {
	log.info("设备窗口:开始初始化");
		//初始化查询

		
		TableNums = Getnum();
		TablePages = TableNums/number;
		if (TableNums%number>0) {
			TablePages++;
		}
		insert(1,number);
		num.setText("1");

		NewAs.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				PageUtil util = new PageUtil();
				util.setPage("/fxml/New/StationNew.fxml", "新增工位");

			}

		});
		//首页
		startbutton.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				num.setText("1");
				pages=1;
				insert(1,number);
			}
		});
		//尾页
		endbutton.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				num.setText(Integer.toString(TablePages));
				pages=TablePages;
				insert(TablePages, number);
			}
		});
		//前一页
		upbutton.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				if(pages>1) {
					pages--;
				}
				num.setText(Integer.toString(pages));
				insert(pages, number);
			}
		});
		//后一页
		downbutton.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				if(pages<TablePages) {
					pages++;
				}
				num.setText(Integer.toString(pages));
				insert(pages, number);
			}
		});
		//跳转
		skip.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				int a = pages;
				try {
					a = Integer.valueOf(num.getText());
					pages =a;
				} catch (Exception e) {
					System.out.println(e);
				}
				num.setText(Integer.toString(pages));
				insert(pages, number);
			}
		});
		log.info("设备窗口:初始化完毕");
	}
	
	//执行分页查询
	private void insert(int pages,int number) {
		try {
			String s = "http://127.0.0.1:8080/station/select?pages="+pages+"&line="+number;
			log.info("设备窗口:执行分页查询:"+s);
			String gethttp = ClientUtil.Gethttp(s);
			log.info("设备窗口:执行分页查询结果:"+gethttp);
			JsonResult jsonobj = ClientUtil.Jsonobj(gethttp, JsonResult.class);
			String string = jsonobj.getData().toString();
			List<StationShow> jsonlist = ClientUtil.Jsonlist(string, StationShow.class);
			TableShow(TableAs,jsonlist);
			tabAddButton();
		} catch (Exception e) {
			log.error("设备窗口:执行分页查询失败:"+e);
		}
		
	}

	//查询总记录数
	private int Getnum() { 
		try {
			String s = "http://127.0.0.1:8080/station/selectSize";
			log.info("设备窗口:查询总记录数:"+s);
			String gethttp = ClientUtil.Gethttp(s);
			log.info("设备窗口:查询总记录数结果:"+gethttp);
			JsonResult jsonobj = ClientUtil.Jsonobj(gethttp, JsonResult.class);
			return (int) jsonobj.getData();
		} catch (Exception e) {
			log.error("设备窗口:查询总记录数失败:"+e);
			return 0;
		}
		
	}

	//将查询结果加入表格
	private void TableShow(TableView tab,List<StationShow> list) {
		tab.getColumns().clear();
		TableColumn id = new TableColumn("ID");
		id.setCellValueFactory(new PropertyValueFactory<>("id"));
		
		TableColumn equipmentName = new TableColumn("所属设备");
		equipmentName.setCellValueFactory(new PropertyValueFactory<>("equipmentName"));
		
		TableColumn StationID = new TableColumn("工位名");
		StationID.setCellValueFactory(new PropertyValueFactory<>("stationname"));
		
		TableColumn numa = new TableColumn("通道a");
		numa.setCellValueFactory(new PropertyValueFactory<>("numa"));
		
		TableColumn numb = new TableColumn("通道b");
		numb.setCellValueFactory(new PropertyValueFactory<>("numb"));
		
		TableColumn remarks = new TableColumn("工位描述");
		remarks.setCellValueFactory(new PropertyValueFactory<>("remarks"));
		
		ObservableList<StationShow> data = FXCollections.observableArrayList();
		tab.setItems(data);
		tab.getColumns().addAll(id,equipmentName,StationID,numa,numb,remarks);
		if (list!=null) {
			for (StationShow stationShow : list) {
				data.add(stationShow);
			}
		}
	}
	
	//为tab增加按钮
	private void tabAddButton() {
		TableColumn<StationShow, Void> updata = new TableColumn<StationShow, Void>("修改");
		Callback<TableColumn<StationShow, Void>, TableCell<StationShow, Void>> callback = new Callback<TableColumn<StationShow,Void>, TableCell<StationShow,Void>>() {
			@Override
			public TableCell<StationShow, Void> call(TableColumn<StationShow, Void> arg0) {
				TableCell<StationShow, Void> cell = new TableCell<StationShow, Void>(){
					Button button =  new Button("修改");
					{
						button.setOnAction(new EventHandler<ActionEvent>() {
							@Override
							public void handle(ActionEvent arg0) {
								StationShow StationShow = getTableView().getItems().get(getIndex());
								CommonUpdata.setStation(StationShow);
								PageUtil util = new PageUtil();
								util.setPage("/fxml/updata/StationNew.fxml", "修改工位信息");
							}
						});
					}
					@Override
					protected void updateItem(Void item, boolean empty) {
						super.updateItem(item, empty);
						if (empty) {
							setGraphic(null);
						} else {
							setGraphic(button);
						}
					}
				};
				return cell;
			}
		};
		TableColumn<StationShow, Void> delete = new TableColumn<StationShow, Void>("删除");
		Callback<TableColumn<StationShow, Void>, TableCell<StationShow, Void>> callback2 = new Callback<TableColumn<StationShow,Void>, TableCell<StationShow,Void>>() {
			@Override
			public TableCell<StationShow, Void> call(TableColumn<StationShow, Void> arg0) {
				TableCell<StationShow, Void> cell = new TableCell<StationShow, Void>(){
					Button button =  new Button("删除");
					{
						button.setOnAction(new EventHandler<ActionEvent>() {

							@Override
							public void handle(ActionEvent arg0) {
								StationShow StationShow = getTableView().getItems().get(getIndex());
								String s = "http://127.0.0.1:8080/station/delete?id="+StationShow.getId();
								ClientUtil.Gethttp(s);
								insert(TablePages, number);
							}
						});
					}
					@Override
					protected void updateItem(Void item, boolean empty) {
						super.updateItem(item, empty);
						if (empty) {
							setGraphic(null);
						} else {
							setGraphic(button);
						}
					}
				};
				return cell;
			}
		};
		updata.setCellFactory(callback);
		TableAs.getColumns().add(updata);
		delete.setCellFactory(callback2);
		TableAs.getColumns().add(delete);
	}

}
